//
//  UISlideImageView.swift
//  viewcontroller_test
//
//  Created by 潘茹燕 on 16/4/2.
//  Copyright © 2016年 海南开工信息技术有限公司. All rights reserved.
//

import UIKit

class UISlideImageView: UIView, UIScrollViewDelegate {

    
    struct Item {
        var urlString: String?
        var image: UIImage?
    }
    
    
    
    
    
    private var scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    private var pageControll = UIPageControl(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    
    
    private var items = [Item]()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupViews()
    }

    
    private func setupViews() {
        
        
        
        scrollView.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollView.pagingEnabled = true
        
        scrollView.contentInset.top = 0
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = true
        scrollView.directionalLockEnabled = true
        
        
        pageControll.pageIndicatorTintColor = UIColor.blackColor()
        pageControll.currentPageIndicatorTintColor = UIColor.redColor()
        
        pageControll.addTarget(self, action: #selector(UISlideImageView.pageSelect(_:)), forControlEvents: UIControlEvents.ValueChanged)
        
        
        
        addSubview(pageControll)
        addSubview(scrollView)
        
        scrollView.delegate = self
        
        bringSubviewToFront(pageControll)
        
    }
    
    func pageSelect(sender: AnyObject?) {
        
        scrollView.setContentOffset(CGPoint(x: CGFloat(pageControll.currentPage) * scrollView.frame.width, y: 0.0), animated: true)
        
    }
    
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        
        if newSuperview == nil {
            
            removeObserver(self, forKeyPath: "bounds")
            
        }
        else {
            addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.New, context: nil)
        }
    }
    
    
    
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        //super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        
        if keyPath == "bounds" {
            
            
            var c = 0
            scrollView.showsHorizontalScrollIndicator = false
            scrollView.showsVerticalScrollIndicator = false
            scrollView.contentInset.top = 0
            
            for view in scrollView.subviews {
                
                if let imageView = view as? UIImageView {
                
                    imageView.frame = CGRect(x: scrollView.bounds.width * CGFloat(c), y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height)
                    
                    c = c + 1
                }
                
            }
            
            
            scrollView.contentSize = CGSize(width: scrollView.bounds.width * CGFloat(c), height: scrollView.bounds.height)
            
            pageControll.frame = CGRect(x: 0, y: self.frame.height - 40.0, width: bounds.width, height: 40)
            
            // BUG 修正，当旋转屏幕的时候，有可能出于两张图片中间
            scrollView.setContentOffset(CGPoint(x: CGFloat(pageControll.currentPage) * scrollView.bounds.width, y: 0), animated: true)
            
            
        }
        
        
    }
    
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let pageWidth = scrollView.frame.size.width;
        //根据当前的坐标与页宽计算当前页码
        let currentPage = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
        
        pageControll.currentPage = Int(currentPage)
        
    }
    
    
    func addImage(image:UIImage) {
        
        let view = UIImageView()
        view.image = image
        addView(view)
    }
    
    
    func addImage(url urlString: String) {
        
        let view = UIAsyncImageView()
        
        view.LoadImageFromURL(urlString)
        addView(view)
        
    }
    
    func addView(view: UIView) {
        
        
        let count = scrollView.subviews.count
        
        
        view.frame = CGRect(x: scrollView.bounds.width * CGFloat(count), y: 0.0, width: scrollView.bounds.width, height: scrollView.bounds.height)
        
        scrollView.addSubview(view)
        scrollView.contentSize = CGSize(width: scrollView.bounds.width * CGFloat(count+1), height: scrollView.bounds.height)
        pageControll.numberOfPages = count + 1
    }
    
}
